package com.ruoyi.miaomiao.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Component
public class ServiceUtil {

    @Value("${server.port}")
    private int serverPort;

    /**
     * 获取当前服务器的IP地址和本次服务运行的port
     * @return
     */
    public String getCurrentServiceUrl(){
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        //将返回一个形如 http://192.168.2.78:8080/ 的字符传，主要用于拼接静态资源（商品封面等文件）的url
        return "http://"+address.getHostAddress() +":"+ serverPort;

    }

}
